Geolocation sample


Windows Runtime, Windows 8.1, Windows Phone 8.1
Services, universal app
Windows RT
en-US
4/2/2014

The Geolocation sample demonstrates how to use the Windows.Devices.Geolocation namespace to get the geographic location of the user's device. An app can use the Geolocation namespace to get the location one time, or it can continuously track the location by getting location update events.

Note  This sample was created using one of the universal app templates available in Visual Studio. It shows how its solution is structured so it can run on both Windows 8.1 and Windows Phone 8.1. For more info about how to build apps that target Windows and Windows Phone with Visual Studio, see Build apps that target Windows and Windows Phone 8.1 by using Visual Studio.

The sample also demonstrates how to:

  • use the Windows.Devices.Geolocation.Geofencing namespace to get notifications when the user's device has entered or left an area of interest.
  • read the StatusChangedEventArgs to handle location status changed events.
  • get the location using a background task.
  • handle doing geofencing as a Background task.
  • use the GeocoordinateSatelliteData to obtain additional information on the quality of the satellite based positioning obtained.
  • display a toast when a background geofencing event has occurred.
  • refresh geofence binding on resume and after removal of a geofences. Note that removal requires subscribing to removal events if a geofence is set as single use or a duration is set.
  • use of the DeviceAccessInformation.AccessChanged event. Change the location permissions from the Settings charm to see the access change events.

Geofences need to be created in the Foreground Geofencing scenario (Scenario 4) and then you can go to Background Geofencing scenario (Scenario 5) to register for background geofencing events.

The Geolocation sample formats and parses time and dates in the en-US locale using the Gregorian calendar and 24-hour clock. To help other locales in entering data the edit fields have a format example shown below the control. For example, Start Time would be entered mm/dd/yyyy hh:mm:ss format. February 2, 2014 at 10:34 pm would be written 2/2/2014 22:34:00. For the Dwell Time and Duration the format is dd:hh:mm:ss so a time span of 7 days, 23 hours, 45 minutes and 55 seconds would be written as 7:23:45:55.

To obtain an evaluation copy of Windows 8.1, go to Windows 8.1.

To obtain an evaluation copy of Microsoft Visual Studio 2013, go to Visual Studio 2013.

Note  For Windows 8 app samples, download the Windows 8 app samples pack. The samples in the Windows 8 app samples pack will build and run only on Microsoft Visual Studio 2012.

Related topics

Windows 8 app samples
Tutorials
How to adjust the distance between location updates
How to respond to location updates
Quickstart: detecting the user's location
Run Windows Store apps in the simulator
API Reference
Windows.Devices.Geolocation
Windows.Devices.Geolocation.Geofencing
Guidelines
Guidelines for location-aware apps

Related technologies

Detecting the user's location

Operating system requirements

Client
Windows 8.1
Server
None supported
Phone
Windows Phone 8.1

Build the sample

  1. Start Microsoft Visual Studio 2013 Update 2 and select File > Open > Project/Solution.
  2. Go to the directory to which you unzipped the sample. Then go to the subdirectory containing the sample in the language you desire - either C++, C#, JavaScript, or Visual Basic. Double-click the Visual Studio 2013 Update 2 Solution (.sln) file.
  3. Select either the Windows or Windows Phone project version of the sample. Press Ctrl+Shift+B, or select Build > Build Solution.

Run the sample

The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.

Deploying the sample

  1. Select either the Windows or Windows Phone project version of the sample.
  2. Select Build > Deploy Solution.

Deploying and running the sample

  1. Right-click either the Windows or Windows Phone project version of the sample in Solution Explorer and select Set as StartUp Project.
  2. To debug the sample and then run it, press F5 or select Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or selectDebug > Start Without Debugging.